var app = new Vue({
    el: '#app',
    data: {
        list: [
            {
                type: 1,
                name: '电子产品',
                child: [{
                    id: 1,
                    name: 'iPhone 7',
                    price: 6188,
                    count: 1,
                    checked: false
                },
                    {
                        id: 2,
                        name: 'iPad Pro',
                        price: 5999,
                        count: 1,
                        checked: false
                    },
                    {
                        id: 3,
                        name: 'MacBook Pro',
                        price: 21488,
                        count: 1,
                        checked: false
                    }],
            },

            {
                type: 2,
                name: '生活用品',
                child: [{
                    id: 1,
                    name: '菜刀',
                    price: 48,
                    count: 1,
                    checked: false
                },
                    {
                        id: 2,
                        name: '砧板',
                        price: 14,
                        count: 1,
                        checked: false
                    },
                    {
                        id: 3,
                        name: '筷子',
                        price: 8,
                        count: 1,
                        checked: false
                    }],
            },
            {
                type: 3,
                name: '果蔬',
                child: [{
                    id: 1,
                    name: '西红柿',
                    price: 8,
                    count: 1,
                    checked: false
                },
                    {
                        id: 2,
                        name: '香蕉',
                        price: 6,
                        count: 1,
                        checked: false
                    },
                    {
                        id: 3,
                        name: '苹果',
                        price: 5,
                        count: 1,
                        checked: false
                    }],
            },


        ]
    },
    computed: {
        totalPrice: function () {
            let total = 0;
            for (let i = 0; i < this.list.length; i++) {
                var item = this.list[i].child;
                for (let j = 0; j < item.length; j++) {
                    if (item[j].checked) {
                        total += item[j].price * item[j].count;
                    }
                }
            }
            //转换为千分位
            return total.toString().replace(/\B(?=(\d{3})+$)/g, ',')
        }
    },
    methods: {
        handleReduce: function (index,index2) {
            let item = this.list[index].child[index2];
            if (item.count === 1)
                return;
            item.count--;
        },
        handleAdd: function (index,index2) {
            this.list[index].child[index2].count++;
        },
        handleRemove: function (index,index2) {
            this.list[index].child.splice(index2, 1);
        },
        checkboxOnClick: function (index,index2) {
            this.list[index].child[index2].checked = !this.list[index].child[index2].checked;
        },
        selectAll: function () {
            for (var i = 0; i < this.list.length; i++) {
                let item = this.list[i].child;
                for (var j =0; j< item.length; j++){
                    item[j].checked = true;
                }
            }
        }
    }
});
